package com.st.warehouse.ui.zxing;




import static com.st.warehouse.ui.zxing.PhotoUtils.GALLERY_REQUEST_CODE;
import static com.st.warehouse.ui.zxing.PhotoUtils.mImageUri;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.st.warehouse.R;


import java.io.FileNotFoundException;


/**
 *
 */
public class CaptureActivity extends Activity {
    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;
    private LinearLayout linearLayout;//根据需求定义
    private String TAG = "CaptureActivity2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        barcodeScannerView = initializeContent();

        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.onResume();

        capture.decode();
    }

    /**
     * Override to use a different layout.
     *
     * @return the DecoratedBarcodeView
     */
    protected DecoratedBarcodeView initializeContent() {
        setContentView(R.layout.my_zxing_capture);
        linearLayout = findViewById(R.id.ll_photo);//选择是否从相册选择
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoUtils.choosePhoto(CaptureActivity.this);
            }
        });
        return (DecoratedBarcodeView)findViewById(R.id.zxing_barcode_scanner);
    }
    //当拍摄照片完成时会回调到onActivityResult 在这里处理照片的裁剪
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
           if(requestCode == GALLERY_REQUEST_CODE){
                // 获取图片
                try {
                    //该uri是上一个Activity返回的
                    mImageUri = data.getData();
                    if(mImageUri!=null) {
                        handlePhoto();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 从相册选择的图片进行扫描
     * @throws FileNotFoundException
     */
    private void handlePhoto() throws FileNotFoundException {

        String imagePath = PhotoUtils.getPicturePathFromUri(CaptureActivity.this, mImageUri);
        //对获取到的二维码照片进行压缩
        final Bitmap bitmap = PhotoUtils.compressPicture(imagePath);
        new Thread(new Runnable() {
            @Override
            public void run() {
                ZXingUtils.syncDecodeQRCode(bitmap, new ZXingUtils.ResultCallBack() {
                    @Override
                    public void callBack(String result) {
                        Intent intent = new Intent();
                        intent.putExtra("SCAN_RESULT",result);
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                });
            }
        }).start();
    }
    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}
